Trò chơi Angry Birds trong UNITY Engine
31.719 lượt xem;
- IntTweenProperty.cs
- genericTypes /
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System;
3 using System.Collections;
4
5
6 public class IntTweenProperty : AbstractTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9
10 private Action<int> _setter;
11
12 protected int _originalEndValue;
13 protected int _startValue;
14 protected int _endValue;
15 protected int _diffValue;
16
17
18 public IntTweenProperty( string propertyName, int endValue, bool isRelative = false ) : base( isRelative )
19 {
20 this.propertyName = propertyName;
21 _originalEndValue = endValue;
22 }
23
24
25 /// <summary>
26 /// validation checks to make sure the target has a valid property with an accessible setter
27 /// </summary>
28 public override bool validateTarget( object target )
29 {
30 // cache the setter
31 _setter = GoTweenUtils.setterForProperty<Action<int>>( target, propertyName );
32 return _setter != null;
33 }
34
35
36 public override void prepareForUse()
37 {
38 // retrieve the getter
39 var getter = GoTweenUtils.getterForProperty<Func<int>>( _ownerTween.target, propertyName );
40
41 _endValue = _originalEndValue;
42
43 // if this is a from tween we need to swap the start and end values
44 if( _ownerTween.isFrom )
45 {
46 _startValue = _endValue;
47 _endValue = getter();
48 }
49 else
50 {
51 _startValue = getter();
52 }
53
54 // prep the diff value
55 if( _isRelative && !_ownerTween.isFrom )
56 _diffValue = _endValue;
57 else
58 _diffValue = _endValue - _startValue;
59 }
60
61
62 public override void tick( float totalElapsedTime )
63 {
64 var easedValue = _easeFunction( totalElapsedTime, _startValue, _diffValue, _ownerTween.duration );
65 _setter( (int)Math.Round( easedValue ) );
66 }
67
68 }
2 using System;
3 using System.Collections;
4
5
6 public class IntTweenProperty : AbstractTweenProperty, IGenericProperty
7 {
8 public string propertyName { get; private set; }
9
10 private Action<int> _setter;
11
12 protected int _originalEndValue;
13 protected int _startValue;
14 protected int _endValue;
15 protected int _diffValue;
16
17
18 public IntTweenProperty( string propertyName, int endValue, bool isRelative = false ) : base( isRelative )
19 {
20 this.propertyName = propertyName;
21 _originalEndValue = endValue;
22 }
23
24
25 /// <summary>
26 /// validation checks to make sure the target has a valid property with an accessible setter
27 /// </summary>
28 public override bool validateTarget( object target )
29 {
30 // cache the setter
31 _setter = GoTweenUtils.setterForProperty<Action<int>>( target, propertyName );
32 return _setter != null;
33 }
34
35
36 public override void prepareForUse()
37 {
38 // retrieve the getter
39 var getter = GoTweenUtils.getterForProperty<Func<int>>( _ownerTween.target, propertyName );
40
41 _endValue = _originalEndValue;
42
43 // if this is a from tween we need to swap the start and end values
44 if( _ownerTween.isFrom )
45 {
46 _startValue = _endValue;
47 _endValue = getter();
48 }
49 else
50 {
51 _startValue = getter();
52 }
53
54 // prep the diff value
55 if( _isRelative && !_ownerTween.isFrom )
56 _diffValue = _endValue;
57 else
58 _diffValue = _endValue - _startValue;
59 }
60
61
62 public override void tick( float totalElapsedTime )
63 {
64 var easedValue = _easeFunction( totalElapsedTime, _startValue, _diffValue, _ownerTween.duration );
65 _setter( (int)Math.Round( easedValue ) );
66 }
67
68 }